<?php namespace system\middleware;

use houdunwang\middleware\build\Middleware;
use system\model\Config as ConfigModel;

class Config implements Middleware
{

    //执行中间件
    public function run ( $next )
    {
        //设置系统配置项
        $this->setSysConfig();
        //微信配置项
        $this->setWeiXinConfig();
        $next();
    }

    //设置微信配置项
    private function setWeiXinConfig ()
    {
        $model = ConfigModel::find( 1 );
        $field = $model ? json_decode( $model[ 'weixin' ] , true ) : [];
        \houdunwang\config\Config::set('wechat',$field);
        //Config::get('wechat.token')
        \Config::set('wechat',array_merge(\Config::get('wechat'),$field));
    }

    //设置系统配置项
    private function setSysConfig ()
    {
        $model = ConfigModel::find( 1 );
        $field = $model ? json_decode( $model[ 'system' ] , true ) : [];
        //p($field);
        //系统v函数，需要看函数，追踪代码
        v( 'config' , $field );
    }
}